package com.redare.dev.smart.generator.service.dao;

import com.redare.dev.smart.generator.sdk.pojo.GeneratorSys;
import com.redare.dev.smart.generator.sdk.pojo.form.GeneratorSysForm;
import com.redare.devframework.common.pojo.Page;
import com.redare.devframework.sql.Sql;
import com.redare.devframework.sql.db.base.SpringJdbcHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 业务系统 DAO
 *
 * @date 2021-08-27 15:19:22
 * Created by yangqiangyu
 */
@Repository(value = "com.redare.dev.smart.generator.dao.GeneratorSysDao")
public class GeneratorSysDao {

    @Autowired
    SpringJdbcHelper jdbcHelper;

    public long addGeneratorSys(GeneratorSysForm.Add form) {
        Sql sql = Sql.insert("generator_sys")
                .field("code", form.getCode())
                .field("name", form.getName())
                .field("createTime", new Date());
        return jdbcHelper.insertReturnFieldNumber(sql).longValue();
    }

    public int[] addGeneratorSys(List<GeneratorSysForm.Add> list) {
        if (list == null || list.isEmpty()) {
            return new int[]{};
        }
        Sql sql = Sql.insert("generator_sys")
                .fields("code", "name", "createTime");
        Date createTime = new Date();
        List<Object[]> params = new ArrayList<>();
        for (GeneratorSysForm.Add item : list) {
            params.add(new Object[]{item.getCode(), item.getName(), createTime});
        }
        return jdbcHelper.batchUpdate(sql, params);
    }

    public int deleteGeneratorSys(GeneratorSysForm.Del form) {
        Sql sql = Sql.delete("generator_sys")
                .where().andEq("id", form.getId());
        return jdbcHelper.update(sql.toString(), sql.getParams().toArray());
    }

    public Page<GeneratorSys> findGeneratorSysPage(GeneratorSysForm.Search form, int curPage, int pageSize, boolean calCount, boolean allList) {
        Sql sql = Sql.select()
                .fieldsIf(StringUtils.isNotBlank(form.getQueryFields()), form.getQueryFields())
                .fieldsIf(StringUtils.isBlank(form.getQueryFields()), "*")
                .from("generator_sys")
                .where()
                .andBLikeFieldsJoinByOrIfNotBlank(
                        form.getKey()
                        , "code"
                        , "name"
                )
                .orderBy().desc("id");
        return jdbcHelper.queryForPageBean(sql.toString(), GeneratorSys.class, sql.getParams().toArray(), curPage, pageSize, calCount, allList);
    }


    public int editGeneratorSys(GeneratorSysForm.Edit form) {
        Sql sql = Sql.update("generator_sys")
                .setIfNotBlank("code", form.getCode())
                .setIfNotBlank("name", form.getName())
                .where()
                .andEq("id", form.getId());
        return jdbcHelper.update(sql.toString(), sql.getParams().toArray());
    }

    public GeneratorSys getGeneratorSys(GeneratorSysForm.Get form) {
        Sql sql = Sql.select()
                .fieldsIf(StringUtils.isNotBlank(form.getQueryFields()), form.getQueryFields())
                .fieldsIf(StringUtils.isBlank(form.getQueryFields()), "*")
                .from("generator_sys")
                .where()
                .andEq("id", form.getId());
        return jdbcHelper.queryForBean(sql.toString(), GeneratorSys.class, sql.getParams().toArray());
    }


}

